/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.maleskoding.jmplurk.ui.components;

import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Petra Barus
 */
public class PlurkTimelineComponent extends Container implements FocusListener {

    private String plurkID = "";

    public PlurkTimelineComponent() {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        init();
    }

    /**
     * Initialize components
     */
    public void init() {
        this.setFocusable(true);
        this.setScrollable(true);
        this.setScrollableX(false);
        //this.setHandlesInput(true);
        this.setSmoothScrolling(true);
    }

    public void clearTimeline() {
        removeAll();
    }

    public String getSelectedPlurkID() {
        return plurkID;
    }

    public void appendTimeline(JSONObject timeline) {
        try {
            JSONObject plurk_users;
            if (!timeline.isNull("plurks_users")) {
                plurk_users = timeline.getJSONObject("plurks_users");
            } else {
                plurk_users = timeline.getJSONObject("plurk_users");
            }
            JSONArray plurks = timeline.getJSONArray("plurks");
            for (int i = 0; i < plurks.length(); i++) {
                JSONObject plurk = plurks.getJSONObject(i);
                JSONObject user = plurk_users.getJSONObject(plurk.getString("owner_id"));
                String plurkID = plurk.getString("plurk_id");
                String plurkUser;
                if (!user.isNull("display_name")) {
                    plurkUser = user.getString("display_name");
                } else {
                    plurkUser = user.getString("nick_name");
                }
                String qualifier = (!plurk.isNull("qualifier_translated")) ? plurk.getString("qualifier_translated") : plurk.getString("qualifier");
                String plurkContent = qualifier + " " + plurk.getString("content_raw");
                String plurkDate = plurk.getString("posted");

                //Creating Style
                Style selectedStyle = new Style();
                selectedStyle.setBgColor(0x000000);
                selectedStyle.setBgTransparency(100);
                selectedStyle.setBgColor(0x000000);
                selectedStyle.setMargin(0, 0, 0, 0);
                selectedStyle.setPadding(0, 0, 0, 0);
                //Creating Container
                Container plurkContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
                plurkContainer.getStyle().setBgTransparency(0);
                plurkContainer.getStyle().setMargin(3, 3, 1, 1);
                plurkContainer.setFocusable(true);
                plurkContainer.setSelectedStyle(selectedStyle);
                plurkContainer.setScrollable(true);
                plurkContainer.setScrollableX(false);
                plurkContainer.putClientProperty("plurkID", plurkID);
                plurkContainer.addFocusListener(this);
                //Creating user label
                Label userLabel = new Label(plurkUser);
                userLabel.getStyle().setMargin(0, 0, 0, 0);
                userLabel.getStyle().setPadding(0, 0, 0, 0);
                userLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
                userLabel.getStyle().setBgTransparency(0);
                //Creating date label
                Label dateLabel = new Label(plurkDate);
                dateLabel.getStyle().setMargin(0, 0, 0, 0);
                dateLabel.getStyle().setPadding(0, 0, 0, 0);
                dateLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
                dateLabel.getStyle().setBgTransparency(0);
                //Creating content label
                TextArea contentText = new TextArea(plurkContent);
                contentText.getStyle().setMargin(0, 0, 0, 0);
                contentText.getStyle().setPadding(0, 0, 0, 0);
                contentText.getStyle().setBgTransparency(0);
                contentText.getStyle().setBorder(Border.createEmpty());
                contentText.setEditable(false);
                contentText.setFocusable(false);
                contentText.setGrowByContent(true);
                contentText.setWidth(Display.getInstance().getDisplayWidth());
                //Adding all components
                plurkContainer.addComponent(userLabel);
                plurkContainer.addComponent(contentText);
                plurkContainer.addComponent(dateLabel);

                //Adding the container
                //this.addComponent(plurkContainer);
                this.addComponent(i, plurkContainer);
            }
        } catch (JSONException ex) {
        }
    }

    public void focusGained(Component cmpnt) {
        plurkID = (String) cmpnt.getClientProperty("plurkID");
    }

    public void focusLost(Component cmpnt) {
    }
}
